﻿<UserControl
    x:Name="this"
    x:Class="Poet.Views.SearchAndReplaceView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Views="clr-namespace:Poet.Views" 
    xmlns:Converters="clr-namespace:Poet.Converters">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\Resources\Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>   
            <Converters:BooleanInvertConverter x:Key="BooleanInvertConverter"/>
            <Converters:MoveButtonCursorConverter x:Key="MoveButtonCursorConverter"/>
            <Converters:ItemsSelectorTextConverter x:Key="ItemsSelectorTextConverter"/>
        </ResourceDictionary>
    </UserControl.Resources>

        <Grid>
        <StackPanel>
            
            <StackPanel.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="Margin" Value="3"/>
                </Style>
                <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
                <Converters:BooleanToVisibilityInvertedConverter x:Key="BooleanToVisibilityInvertedConverter"/>
            </StackPanel.Resources>

            <TextBlock Text="{Localizing TextPattern}" Visibility="{Binding Options.UseRegularExpression, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"/>
            <ComboBox Margin="3" IsEditable="True" Name="TextPatternComboBox" Text="{Binding TextPattern, Mode=TwoWay}" ItemsSource="{Binding RecentTextPatterns}" Visibility="{Binding Options.UseRegularExpression, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"/>

            <TextBlock Text="{Localizing RegularExpressionPattern}" Visibility="{Binding Options.UseRegularExpression, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <ComboBox Margin="3" IsEditable="True" Name="RegexPatternComboBox" Text="{Binding RegexPattern, ValidatesOnDataErrors=True, Mode=TwoWay}" ItemsSource="{Binding RecentRegexPatterns}" Visibility="{Binding Options.UseRegularExpression, Converter={StaticResource BooleanToVisibilityConverter}}"/>

            <TextBlock Text="{Localizing ReplacePattern}" Visibility="{Binding IsReplaceMode, ElementName=this, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <ComboBox Margin="3" IsEditable="True" Name="ReplacePatternComboBox" Text="{Binding RegexPattern, ValidatesOnDataErrors=True, Mode=TwoWay}" ItemsSource="{Binding RecentReplacePatterns}" Visibility="{Binding IsReplaceMode, ElementName=this, Converter={StaticResource BooleanToVisibilityConverter}}"/>

            <StackPanel Orientation="Horizontal">
                <TextBlock>
                    <TextBlock Margin="0" Text="{Localizing LookInItem}"/>
                    <TextBlock Margin="0" Text="{Binding ActiveDocumentView.Title}" FontStyle="Italic"/>
                </TextBlock>
            </StackPanel>
            
            <ComboBox
                Margin="3"
                IsEditable="True"
                Name="SearchItemsComboBox"
                DataContext="{Binding LookInViewModel}"
                ItemsSource="{Binding Items}"
                SelectionChanged="Selector_OnSelectionChanged"
                Template="{StaticResource SearchComboBoxTemplate}"
                ItemContainerStyle="{StaticResource SearchComboBoxItemStyle}">     
                                
                <ComboBox.Text>
                    <MultiBinding Converter="{StaticResource ItemsSelectorTextConverter}" Mode="OneWay">
                        <Binding Path="SelectedItems.Count" Mode="OneWay"/>
                        <Binding Path="Items.Count" Mode="OneWay"/>
                    </MultiBinding>
                </ComboBox.Text>
                
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <SelectableItem
                            Padding="3,2"
                            x:Name="SelectableItem"
                            Background="Transparent"    
                            MouseMove="SelectableItem_OnPreviewMouseMove"
                            PreviewMouseDown="SelectableItem_OnMouseDown" 
                            Cursor="{Binding ElementName=SearchItemsComboBox, Path=DataContext.ItemForMove, Converter={StaticResource MoveButtonCursorConverter}}"
                            SelectedItems="{Binding DataContext.SelectedItems, ElementName=SearchItemsComboBox}">
                            
                            <Grid Width="Auto">
                                
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="16"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>

                                <CheckBox 
                                    Grid.Column="0" 
                                    Name="CheckBox"
                                    IsHitTestVisible="False"
                                    IsChecked="{Binding IsSelected, Mode=TwoWay, ElementName=SelectableItem}" 
                                    IsEnabled="{Binding IsChecked, Mode=OneWay, ElementName=RadioButton, Converter={StaticResource BooleanInvertConverter}}">
                                    <TextBlock Text="{Binding Title}"/>
                                </CheckBox>

                                <Border
                                    Grid.Column="1"
                                    Name="MoveButton"
                                    Margin="2"
                                    CornerRadius="2"   
                                    Background="Beige"
                                    PreviewMouseDown="SelectableItem_OnMouseDown"
                                    ToolTipService.InitialShowDelay="4000"
                                    ToolTipService.BetweenShowDelay="4000"
                                    ToolTip="{Localizing MoveDocument}"/>
                                
                                <RadioButton 
                                    Grid.Column="2"
                                    Name="RadioButton"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Center" 
                                    PreviewMouseDown="SetActiveItem" 
                                    IsChecked="{Binding IsActiveDocument, Mode=OneWay}"
                                    ToolTipService.InitialShowDelay="4000"
                                    ToolTipService.BetweenShowDelay="4000"
                                    ToolTip="{Localizing SetActiveDocument}"/>
                                
                            </Grid>

                        </SelectableItem>

                        <DataTemplate.Triggers>

                            <DataTrigger Binding="{Binding IsActiveDocument}" Value="True">
                                <Setter TargetName="CheckBox" Property="Foreground" Value="Red"/>
                            </DataTrigger>

                            <DataTrigger Binding="{Binding IsActiveDocument}" Value="True">
                                <Setter TargetName="CheckBox" Property="IsChecked" Value="True"/>
                            </DataTrigger>

                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
                                    
            </ComboBox>
            
            <Views:SearchOptionsView DataContext="{Binding Options}" Margin="3"/>

            <StackPanel Margin="3">
                <StackPanel.Resources>
                    <Style TargetType="Button">
                        <Setter Property="Margin" Value="3"/>
                        <Setter Property="Width" Value="50"/>
                    </Style>
                </StackPanel.Resources>                
                                
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                    <TextBlock Text="{Bonding {Localizing Find}, ':'}" VerticalAlignment="Center"/>
                    <Button Content="{Localizing Prev}" Command="{Binding Find}" CommandParameter="False" />
                    <Button Content="{Localizing Next}" Command="{Binding Find}" CommandParameter="True" />
                </StackPanel>

                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Visibility="{Binding IsReplaceMode, ElementName=this, Converter={StaticResource BooleanToVisibilityConverter}}">
                    <TextBlock Text="{Bonding {Localizing Replace}, ':'}" VerticalAlignment="Center"/>
                    <Button Content="{Localizing Prev}" Command="{Binding Replace}" CommandParameter="False"/>
                    <Button Content="{Localizing Next}" Command="{Binding Replace}" CommandParameter="True"/>
                </StackPanel>
                
            </StackPanel>

        </StackPanel>
    </Grid>
</UserControl>
